<?php
require_once "./sqlop.php";
require_once "./gfun.php";
require_once "./request.php";
require_once "./initiative.php";
class jicunbao
{
	static function index($data,$clientInfo){
		if(strlen($data)>10&&ord($data[0])==58&&ord($data[1])==163&&ord($data[2])==0&&ord($data[3])==0&&ord($data[4])==0){//判断是否机器命令
			if(ord($data[5])==255){return mln::index($data,$clientInfo);}//美莱恩照明
			$addrlength=gfun::getaddrlength($data);
			$macno=gfun::getmacno($data);
			$cindex=$addrlength+8;
			$cindexA=$addrlength+9;
			if(ord($data[$cindex])==9){//机器主动指令
				if(ord($data[$cindexA])!=2)
					initiative::savemachinecommand($data,$clientInfo["address"],$clientInfo["port"],0);
				if(ord($data[$cindexA])==1){//登录01
					$command=self::logon($data,$clientInfo);
					return $command;
				}
				if(ord($data[$cindexA])==2){//心跳02
					$command=self::jump($data,$clientInfo);
					return $command;
				}
				if(ord($data[$cindexA])==5){//密码输入上传05
					$command=self::passwordin($data,$clientInfo);
					return $command;
				}
			}
			else{//机器应答指令
				$addrlength=gfun::getaddrlength($data);
				$macno=gfun::getmacno($data);
				$content='机器指令应答';
				initiative::savemachinecommand($data,$clientInfo["address"],$clientInfo["port"],0,$content);
			}
		}
		else{
			return gfun::strtohex("Received:".$data);
		}
	}
	
	static function logon($data,$clientInfo){//登录
		$command=gfun::getprecom($data)."0003090100";
		//保存记录格式、机器类型、通讯方式,协议控制字
		$addrlength=gfun::getaddrlength($data);
		$macno=gfun::getmacno($data);
		$rtype=substr(gfun::strtohex($data),($addrlength+34)*2,(strlen($data)-$addrlength-36)*2);
		$mtype=substr(gfun::strtohex($data),($addrlength+10)*2,8);
		if(strlen($macno)>11&&substr($macno,0,11)=="52020170224"){
			request::post3("huibiao.app.xiaozhuschool.com","/api/logon",http_build_query(array("macno"=>$macno)));//异步发POST，不阻塞等待接收
		}else
			self::savelist($macno,$clientInfo['address'],$clientInfo["port"],ord($data[5]),$rtype,$mtype);
		return $command.gfun::getcrc16($command);
	}
	static function jump($data,$clientInfo){//心跳
		self::updatebeat($data,$clientInfo);
		
		$command=gfun::getprecom($data)."000B090201".gfun::gettime(time());
		return $command.gfun::getcrc16($command);
	}
	static function record($data){//数据上传
		$command=gfun::getprecom($data)."00050903".gfun::getchildchar($data,0,2)."00";
		//self::saverecord($data)
		return $command.gfun::getcrc16($command);
	}
	static function passwordin($data,$clientInfo){//密码输入
		$macno=gfun::getmacno($data);
		$addrlength=gfun::getaddrlength($data);
		$password=hex2bin(substr(gfun::strtohex($data),($addrlength+10)*2,16));
		$list=sqlop::select("* from m_storages where macno='$macno' and password='$password' order by caseno");
		if(!empty($list)){
			while ($row = $list->fetch_assoc()){
				$command=initiative::getcommand($macno,'0A06','00000000010005'.substr('0'.dechex($row['caseno']),-2),'1');//储物箱开门命令
				initiative::send($macno,$clientInfo['address'],$clientInfo["port"],$command,"密码控制开门");
				//sqlop::query("delete from m_storages where macno='$macno' and password='$password'");
				$post=array("macno"=>$macno,'caseno'=>$row['caseno']);
				$posturl=sqlop::select("* from m_url where macno='$macno'");
				if(!empty($posturl)){
					while ($row = $posturl->fetch_assoc()){
						if($row['sign'])$post['sign']=$row['sign'];
						request::post3($row['url'],$row['path'],http_build_query($post));//异步发POST，不阻塞等待接收
					}
				}
			}
		}
		return bin2hex(initiative::getcommand($macno,'0905','00','1'));
	}
	static function savelist($macno,$ip,$port,$ptype,$rtype,$mtype){
		$ret=sqlop::select("* from jqy_facilities where facilities_code='".$macno."'");
		if(empty($ret)||$ret->num_rows==0){
			$sqlstr="jqy_facilities (facilities_code,ip,port,ptype,rtype,mtype,lastlogon) values('".$macno."','".$ip."',".$port.",".$ptype.",'".$rtype."',".$mtype.",".time().")";
			sqlop::insert($sqlstr);
		}
		else{
			$sqlstr="jqy_facilities set ip='".$ip."',port=".$port.",ptype=".$ptype.",rtype='".$rtype."',mtype=".$mtype.",lastlogon=".time()." where facilities_code='".$macno."'";
			sqlop::update($sqlstr);
		}
	}
	static function updatebeat($data,$clientInfo){//更新保存温度湿度
		$addrlength=gfun::getaddrlength($data);
		$macno=gfun::getmacno($data);
		$sql="jqy_facilities set lastbeat=".time().",ip='".$clientInfo['address']."',port='".$clientInfo['port']."'";
		
		$sql.=" where facilities_code='".$macno."'";
		echo $sql.PHP_EOL;
		$ret=sqlop::update($sql);
	}
}